package com.newegg.datafeed.adserving.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.newegg.datafeed.adserving.domain.Channel;
import com.newegg.datafeed.adserving.mapper.ChannelMapper;
import com.newegg.datafeed.adserving.mapper.UserChannelMapper;

@Service
@Transactional
public class ChannelService {
	
	@Autowired
	ChannelMapper channelMapper;
	@Autowired
	UserChannelMapper userChannelMapper;
	
	public List<Channel> all() {
		return channelMapper.getAll();
	}
	
	public List<String> allKeys() {
		return channelMapper.allKeys();
	}
	
	public List<String> getUserChannels(String user){
		return userChannelMapper.getByUser(user);
	}
	
	public Channel get(String id){
		return channelMapper.get(id);
	}
	
	public boolean exist(String id){
		Channel channel = get(id);
		if(channel == null){ return false; }
		return true;
	}

	public Channel save(Channel channel) {
		if(exist(channel.getId())){
			throw new RuntimeException("this channel has exist");
		}
		channelMapper.insert(channel);
		return channel;
	}
	
	public Channel update(Channel channel) {
		if(!exist(channel.getId())){
			throw new RuntimeException("this channel has not exist");
		}
		channelMapper.update(channel);
		return channel;
	}

	public void delete(String id) {
		channelMapper.delete(id);
	}
}
